package com.ziyun.erp.web.modules.quoteProductAttributeOptionRelation.controller;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.modules.goodsCategory.entity.GoodsCategoryEntity;
import com.ziyun.erp.modules.goodsCategory.service.GoodsCategoryService;
import com.ziyun.erp.modules.quoteProductAttributeOptionRelation.vo.QuoteAttributeSpecialVo;
import com.ziyun.erp.modules.quoteProductAttributeOptionRelation.vo.QuoteOptionSpecialVo;
import io.renren.common.exception.RRException;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.quoteProductAttributeOptionRelation.entity.QuoteProductAttributeOptionRelationEntity;
import com.ziyun.erp.modules.quoteProductAttributeOptionRelation.service.QuoteProductAttributeOptionRelationService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 产品属性选项特殊关系表
 *
 * @author zh
 * @email 296588519@qq.com
 * @date 2018-09-13 14:06:22
 */
@RestController
@RequestMapping("quoteProductAttributeOptionRelation/quoteproductattributeoptionrelation")
public class QuoteProductAttributeOptionRelationController {
    @Autowired
    private QuoteProductAttributeOptionRelationService quoteProductAttributeOptionRelationService;

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("quoteProductAttributeOptionRelation:quoteproductattributeoptionrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = quoteProductAttributeOptionRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 属性特规列表
     */
    @RequestMapping("/queryAttributeSpecialList")
    //@RequiresPermissions("quoteProductAttributeOptionRelation:quoteproductattributeoptionrelation:list")
    public R queryAttributeSpecialList(@RequestBody Map<String, Object> params){
        PageUtils page = quoteProductAttributeOptionRelationService.queryAttributeSpecialPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 新增特规所需初始数据
     */
    @RequestMapping("/addInfo")
    public R addInfo(){
        // 先获取报价产品分类
        List<GoodsCategoryEntity> categoryList = goodsCategoryService.selectList(new EntityWrapper<GoodsCategoryEntity>()
                .in("goods_type",new ArrayList<String>(Arrays.asList("print", "fast"))));
        QuoteAttributeSpecialVo quoteAttributeSpecial = new QuoteAttributeSpecialVo();
        quoteAttributeSpecial.setAttributeMustChoice(0);
        quoteAttributeSpecial.setAttributeDisplay(1);
        quoteAttributeSpecial.setAttributeIgnoreDefinition(0);
        return R.ok().put("categoryList", categoryList).put("quoteAttributeSpecial",quoteAttributeSpecial);
    }

    /**
     * 根据分类 部件 查询 模板表属性
     * @param categoryId
     * @param partsId
     * @return
     */
    @RequestMapping("/getAttributes")
    public R getAttributes(@RequestParam Integer categoryId, @RequestParam(required = false) Integer partsId){
        if(categoryId == null){
            throw new RRException("请先选择分类!");
        }
        Map<String ,Object> paramsMap = new HashMap<>();
        paramsMap.put("categoryId",categoryId);
        paramsMap.put("partsId",partsId);
        List<Map<String,Object>> attributeList = quoteProductAttributeOptionRelationService.getAttributes(paramsMap);

        return R.ok().put("attributeList", attributeList);
    }

    /**
     * 保存/修改 属性特规
     */
    @RequestMapping("/saveOrUpdateAttributeSpecial")
    //@RequiresPermissions("quoteProductAttributeOptionRelation:quoteproductattributeoptionrelation:save")
    public R save(@RequestBody QuoteAttributeSpecialVo quoteAttributeSpecialVo){
        quoteProductAttributeOptionRelationService.saveOrUpdateAttributeSpecial(quoteAttributeSpecialVo);

        return R.ok("保存成功！");
    }



    /**
     * 属性特规信息
     */
    @RequestMapping("/attributeSpecialInfo/{id}")
    //@RequiresPermissions("quoteProductAttributeOptionRelation:quoteproductattributeoptionrelation:info")
    public R info(@PathVariable("id") Integer id){
        Map<String, Object> rs = quoteProductAttributeOptionRelationService.getAttributeSpecialInfo(id);
        return R.ok(rs);
    }

    /*==================选项特规 部分=======================*/
    /**
     * 选项特规列表
     */
    @RequestMapping("/queryOptionSpecialList")
    //@RequiresPermissions("quoteProductAttributeOptionRelation:quoteproductattributeoptionrelation:list")
    public R queryOptionSpecialList(@RequestBody Map<String, Object> params){
        PageUtils page = quoteProductAttributeOptionRelationService.queryOptionSpecialPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 新增特规所需初始数据
     */
    @RequestMapping("/addOptionSpecialInfo")
    public R addOptionSpecialInfo(){
        // 先获取报价产品分类
        List<GoodsCategoryEntity> categoryList = goodsCategoryService.selectList(new EntityWrapper<GoodsCategoryEntity>()
                .in("goods_type",new ArrayList<String>(Arrays.asList("print", "fast"))));
        QuoteOptionSpecialVo quoteOptionSpecial = new QuoteOptionSpecialVo();
        quoteOptionSpecial.setOptionMustChoice(0);
        quoteOptionSpecial.setOptionIgnoreDefinition(0);
        return R.ok().put("categoryList", categoryList).put("quoteOptionSpecial",quoteOptionSpecial);
    }

    /**
     * 根据分类 部件 属性 查询 选项
     * @param categoryId
     * @param partsId
     * @return
     */
    @RequestMapping("/getOptions")
    public R getOptions(@RequestParam Integer categoryId, @RequestParam Integer attributeId, @RequestParam(required = false) Integer partsId){
        if(categoryId == null){
            throw new RRException("请先选择分类!");
        }
        Map<String ,Object> paramsMap = new HashMap<>();
        paramsMap.put("categoryId",categoryId);
        paramsMap.put("partsId",partsId);
        paramsMap.put("attributeId",attributeId);
        List<Map<String,Object>> optionList = quoteProductAttributeOptionRelationService.getOptions(paramsMap);

        // 查询当前分类/部件下 所有属性选项
        paramsMap.remove("attributeId");
        List<Map<String,Object>> optionAllList = quoteProductAttributeOptionRelationService.getOptions(paramsMap);

        return R.ok().put("optionList", optionList).put("optionAllList",optionAllList);
    }

    /**
     * 保存/修改 选项特规
     */
    @RequestMapping("/saveOrUpdateOptionSpecial")
    //@RequiresPermissions("quoteProductAttributeOptionRelation:quoteproductattributeoptionrelation:save")
    public R saveOrUpdateOptionSpecial(@RequestBody QuoteOptionSpecialVo optionSpecialVo){
        quoteProductAttributeOptionRelationService.saveOrUpdateOptionSpecial(optionSpecialVo);

        return R.ok("保存成功！");
    }

    /**
     * 选项特规信息
     */
    @RequestMapping("/optionSpecialInfo/{id}")
    //@RequiresPermissions("quoteProductAttributeOptionRelation:quoteproductattributeoptionrelation:info")
    public R OptionSpecialInfo(@PathVariable("id") Integer id){
        Map<String, Object> rs = quoteProductAttributeOptionRelationService.getOptionSpecialInfo(id);
        return R.ok(rs);
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("quoteProductAttributeOptionRelation:quoteproductattributeoptionrelation:update")
    public R update(@RequestBody QuoteProductAttributeOptionRelationEntity quoteProductAttributeOptionRelation){
        ValidatorUtils.validateEntity(quoteProductAttributeOptionRelation);
        quoteProductAttributeOptionRelationService.updateAllColumnById(quoteProductAttributeOptionRelation);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("quoteProductAttributeOptionRelation:quoteproductattributeoptionrelation:delete")
    public R delete(@RequestBody Integer[] ids){
        quoteProductAttributeOptionRelationService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
